package com.example.hehecat.yuejian1.mvp.presenter;

import android.util.Log;

import com.example.hehecat.yuejian1.mvp.contract.GameInfoContract;
import com.example.hehecat.yuejian1.mvp.model.GameInfoModel;
import com.example.hehecat.yuejian1.mvp.model.bean.SuccessBean;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.support.constraint.Constraints.TAG;

public class GameInfoPresenter implements GameInfoContract.Presenter {
    GameInfoContract.Model model;
    GameInfoContract.View view;

    public GameInfoPresenter(GameInfoContract.View view) {
        this.view = view;
        model = new GameInfoModel();
    }

    @Override
    public void CollectGame(int user_id, int game_id) {
        model.collectGame(user_id, game_id, new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.body().getSuccess() == 1) {
                    view.showMsg("收藏成功");
                } else if(response.body().getSuccess() == 0) {
                    view.showMsg("取消收藏");
                } else {
                    Log.d(TAG, "onResponse: userid "+user_id+" gameid "+game_id);
                    view.showMsg(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void isCollected(int user_id, int game_id) {
        model.isGameCollect(user_id, game_id, new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.body().getSuccess()==1) {
                    view.setCollect();
                }else if (response.body().getSuccess()==0) {
                    view.setUnCollect();
                }else {
                    view.showMsg("操作失败");
                }
            }

            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void gameEnroll(int game_id, int user_id) {
        model.gameEnroll(game_id, user_id, new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.body().getSuccess()==1){
                    view.showMsg("报名成功");
                }else{
                    view.showMsg(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {
                Log.d(TAG, "onFailure: gameenroll" + t.getMessage());
            }
        });
    }
}
